<?php
/**
 * Validator
 * - Only validate the content of text and textarea fields
 * - Check the type of data, return true if correct, return false otherwise
 * @author RUNSYSTEM
 * @since version - Aug 04, 2011
 */
class Validator {
	/**
	 * isEmail
	 * - Return true if string $data is an email address format
	 * - Otherwise, return false
	 */
	public static function isEmail($data) {
		if ($data == "") {
			return true;
		}
		if (preg_match("/^[a-zA-Z0-9_\.\-=]+@[a-zA-z0-9_\-=]+\.[a-zA-Z0-9_\.\-=]+$/", $data)) {
			return true;
		}
		return false;
	}
	/**
	 * isUrl
	 * - Return true if string $data is an URL format
	 * - Otherwise, return false
	 * - This function if fetch from http://www.phpcentral.com/208-url-validation-php.html
	 */
	public function isUrl($data) {
		if ($data == "") {
			return true;
		}
		$urlregex = "^(https?)\:\/\/";
		if (eregi($urlregex, $data)) {
			return true;
		}
		return false;
	}
	/**
	 * isPostCode
	 * - Return true if string $data is a post code format in JP
	 * - Otherwise, return false
	 */
	public function isPostCode($data) {
		if ($data == "") {
			return true;
		}
		if (eregi("^[0-9]{3}([.-][0-9]{4})$", $data)) {
			return true;
		}
		return false;
	}
	/**
	 * isTelOrFax
	 * - Return true if $data is telephone number of fax number
	 * - Otherwise, return false
	 */
	public function isTelOrFax($data) {
		if ($data == "") {
			return true;
		}
		if (eregi("^[0-9]{1,}([.-][0-9]{1,}){0,}$", $data)) {
			return true;
		}
		return false;
	}
	/**
	 * isNumber
	 * - Return true if string $data is a number format
	 * - Otherwise, return false
	 */
	public static function isNumber($data) {
		if ($data == "") {
			return true;
		}
		if (preg_match("/^-{0,1}[0-9]*\.{0,1}[0-9]*$/", $data)) {
			return true;
		}
		return false;
	}
	/**
	 * isInteger
	 * - Return true if string $data is an integer format
	 * - Otherwise, return false
	 */
	public function isInteger($data) {
		if ($data == "") {
			return true;
		}
		if (preg_match("/^-{0,1}[0-9]*$/", $data)) {
			return true;
		}
		return false;
	}
	/**
	 * _isInteger
	 * - Return true if string $data is an integer format
	 * - Otherwise, return false
	 */
	public function _isInteger($data) {
		if ($data) {
			if (preg_match("/^-{0,1}[0-9]*$/", $data)) {
				return true;
			}
		}
		return false;
	}
	/**
	 * isFloat
	 */
	public function isFloat($numberCheck, $charactersRound) {
		if ($numberCheck == "") {
			return true;
		}
		if (preg_match("/^[-0-9]{1,}+(.[0-9]{1,$charactersRound})?$/", $numberCheck)) {
			return true;
		}
		return false;
	}
	/**
	 * isIntegerPositive
	 * - Return true if string $data is an integer positive format
	 * - Otherwise, return false
	 */
	public static function isIntegerPositive($data) {
		if ($data == "") {
			return true;
		}
		if (preg_match("/^[0-9]*$/", $data)) {
			return true;
		}
		return false;
	}
	/**
	 * isIntegerPositiveAndMinus
	 * - Return true if string $data is an integer positive and minus format
	 * - Otherwise, return false
	 */
	public function isIntegerPositiveAndMinus($data) {
		if ($data == "") {
			return true;
		}
		if (preg_match("/^[0-9]+(-[0-9]+)?(-[0-9]+)?(-[0-9]+)?$/", $data)) {
			return true;
		}
		return false;
	}
    /**
     * isIntegerPositiveOrMinus
     * - Return true if string $data is an integer positive or minus format
     * - Otherwise, return false
     */
    public function isIntegerPositiveOrMinus($data) {
        if ($data == "") {
            return true;
        }
        if (preg_match("/^[0-9-%]+$/", $data)) {
            return true;
        }
        return false;
    }
	/**
	 * isIntegerPositiveAndDot
	 * - Return true if string $data is an integer positive and dot format
	 * - Otherwise, return false
	 */
	public function isIntegerPositiveAndDot($data) {
		if ($data == "") {
			return true;
		}
		if (preg_match("/^[0-9]+(\.[0-9]+)?$/", $data)) {
			return true;
		}
		return false;
	}
	/**
	 * isIntegerPositiveAndAlphabet
	 * - Return true if string $data is an integer positive and alphabet format and space
	 * - Otherwise, return false
	 */
	public function isIntegerPositiveAndAlphabet($data) {
		if ($data == "") {
			return true;
		}
		if (preg_match("/^[0-9a-zA-Z ]+$/", $data)) {
			return true;
		}
		return false;
	}
	/**
	 * isWord
	 * - Return true if string $data is a continuous word
	 * - Otherwise, return false
	 */
	public function isWord($data) {
		if ($data == "") {
			return true;
		}
		if (preg_match("/^[a-zA-Z0-9_@\.\+\-]+$/", $data)) {
			return true;
		}
		return false;
	}
	/**
	 * isDate
	 * - Return true if string $data is a continuous word
	 * - Otherwise, return false
	 */
	public function isDate($data, $sep = "-") {
		list($y, $m, $d) = split($sep, $data);
		$m = sprintf("%02d", $m);
		$d = sprintf("%02d", $d);
		$data = $y . $sep . $m . $sep . $d;
		$strValidate = "/^(\d\d\d\d)" . $sep . "(\d\d)" . $sep . '(\d\d)$/';
		preg_match($strValidate, $data, $ary);
		return checkdate($ary[2],$ary[3],$ary[1]);
	}
	/**
	 * _isDate
	 * - Return true if string $data is a continuous word
	 * - Otherwise, return false
	 */
	public function _isDate($data, $sep = "-") {
		
		list($y, $m, $d) = split($sep, $data);
		if (Validator::_isInteger($y) && Validator::_isInteger($m) && Validator::_isInteger($d)) {
			return checkdate($m, $d, $y);
		}
		return false;
	}

	/**
	 * isEmailMobileDomain
	 * - Check email mobile domain
	 * @param String $strEmail
	 * @return boolean
	 */
	public function isEmailMobileDomain($strEmail) {
		global $domainMobile;
		$start = strpos($strEmail, '@');
		$strDomain = substr($strEmail, $start + 1);
		if (in_array($strDomain, $domainMobile)) {
			return true;
		}
		return false;
	}
	
	/*
	 * check if is alphabet a-z , A->Z
	 */
	public function isAlphabet($str,$encoding=""){
		if ($encoding==""){
			$encoding=_SYSTEM_ENCODING_;
		}	
		if ($encoding!=""){
			mb_regex_encoding($encoding);
		}
		//change full-size space to half-size space
		$str= mb_convert_kana($str,"s",$encoding);
		$str=trim($str);		
		if (mb_ereg_match("^[a-zA-Z]+$",$str)){
			return true;
		}else{
			return false;
		}
	}
	
	/*
	 * check if is alphabetNumeric
	 */
	public function isAlphabetNumeric($str,$encoding=""){
		if ($encoding==""){
			$encoding=_SYSTEM_ENCODING_;
		}	
		if ($encoding!=""){
			mb_regex_encoding($encoding);
		}
		//change full-size space to half-size space
		$str= mb_convert_kana($str,"s",$encoding);
		$str=trim($str);		
		if (mb_ereg_match("^[a-zA-Z0-9]+$",$str)){
			return true;
		}else{
			return false;
		}
	}
    /**
     * isValidIp
     * - Check IP address
     * @param string $ipAddress
     * @return Boolean If is IP address return true; else return false 
     */
    public function isValidIp($ipAddress) {
        $arrayIp = explode(".", $ipAddress);
        if (count($arrayIp) != 4){
            return false;
        }
        if (!eregi("^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$", $ipAddress)){
                return false;
        }
        for($i = 0; $i < 4; $i ++) {
           
            if ($arrayIp[$i] > 255) {
                return false;
            }
        }
        return true;
    }
    /**
     * isValidIpOrther
     * - Check IP address
     * @param string $ipAddress
     * @return Boolean If is IP address return true; else return false 
     */
    public function isValidIpOrther($ipAddress) {
        $arrayIp = explode(".", $ipAddress);
        if (count($arrayIp) != 4){
            return false;
        }
        if (!eregi("^[A-Z0-9]{1,3}\.[A-Z0-9]{1,3}\.[A-Z0-9]{1,3}\.[A-Z0-9]{1,3}$", $ipAddress)){
                return false;
        }
        for($i = 0; $i < 4; $i ++) {
           
            if ($arrayIp[$i] > 255) {
                return false;
            }
        }
        return true;
    }
    
	/**
	 * validateTypeImage
	 * - validate image file extension
	 * @param String $fileName
	 * @return Boolean
	 */
	function validateTypeImage($fileName) {
		if (preg_match('/\.(jpg|jpeg|jpe|jfif|gif|png|swf)$/i', strtolower($fileName))) {
			return true;
		} else {
			return false;
		}
	}
}

?>